package com.smrs.goods.dao;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Query;

import com.jof.framework.dao.hibernate.BaseDAOHibernateImpl;
import com.jof.framework.util.Pager;
import com.smrs.goods.model.GoodsSkuModel;

public class GoodsSkuDao extends BaseDAOHibernateImpl<GoodsSkuModel, Long>{
	
	public List<GoodsSkuModel> getByItemId(Long itemId){
		String queryString = " from GoodsSkuModel where itemId = " + itemId;
		Query query = this.createHQLQuery(queryString, new Object[]{});
		List list = query.list();
		return list;
	}
	
	public  Pager<GoodsSkuModel> findBySku(GoodsSkuModel sku, Pager<GoodsSkuModel> pager){
		String queryString = " from GoodsSkuModel where 1=1";
		if(StringUtils.isNotEmpty(sku.getName())){
			queryString=queryString+" and name like '%" + sku.getName()+"%'";
		}
		if(StringUtils.isNotEmpty(sku.getCode())){
			queryString=queryString+" and code like '%" + sku.getCode()+"%'";
		}
		if(sku.getItemId()!=null){
			queryString=queryString+" and itemId = " +sku.getItemId();
		}
		Query query = this.createHQLQuery(queryString, new Object[]{});
		this.setPageParameterToQuery(query, pager);
		Long count=this.countHQLResult(queryString, new Object[]{});
		List list = query.list();
		Pager<GoodsSkuModel> tempPager = Pager.cloneFromPager(pager, count, list);
		return tempPager;
	} 
}
